const app = getApp();
const util = require('../../../utils/util.js');
const lbbService = require('../../../pages/service/service.js');
const lbbTaskService = require('../service/task-service.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userList:[],
        yearList:[
            new Date().getYear()+1900,
            new Date().getYear()+1900-1,
            new Date().getYear()+1900-2,
            new Date().getYear()+1900-3,
            new Date().getYear()+1900-4,
            new Date().getYear()+1900-5,
            new Date().getYear()+1900-6,
            new Date().getYear()+1900-7,
            new Date().getYear()+1900-8,
            new Date().getYear()+1900-9,
        ],
        monthList:['全部月份','01','02','03','04','05','06','07','08','09','10','11','12'],
        cityShow: '0',
        userName: '全部师傅',
        userId:-1,
        selectYear:new Date().getYear()+1900,
        selectMonth:'全部月份',
        user_animationData: {},
        year_animationData: {}, 
        month_animationData: {}, 
    },
    //创建动画
    toggleAnimation: function(angle) {
        var animation = wx.createAnimation({
          timingFunction: "ease"
        })
        this.animation = animation;
        animation.rotate(angle).step()
    },
    userToggle: function() {
        this.pickUp()
        if (this.data.cityShow=='4') {
          this.setData({
            cityShow: '0',
          })
        } else {
          this.toggleAnimation(180)
          this.setData({
            cityShow: '4',
          })
        }
        this.setData({
          user_animationData: this.animation.export()
        })
    },
    yearToggle: function() {
        this.pickUp()
        if (this.data.cityShow=='5') {
          this.setData({
            cityShow: '0',
          })
        } else {
          this.toggleAnimation(180)
          this.setData({
            cityShow: '5',
          })
        }
        this.setData({
          year_animationData: this.animation.export()
        })
    },
    monthToggle: function() {
        this.pickUp()
        if (this.data.cityShow=='6') {
          this.setData({
            cityShow: '0',
          })
        } else {
          this.toggleAnimation(180)
          this.setData({
            cityShow: '6',
          })
        }
        this.setData({
          month_animationData: this.animation.export()
        })
    },
    setUser:function (e) {
        console.log("setUser-->",e)
       
        let userIndex = e.target.dataset.index; //当前点击的索引
        let userName = e.target.dataset.name;
        let userId = e.target.dataset.id;
        this.toggleAnimation(0)
        this.setData({
            cityShow: '0',
            userIndex,
            userId,
            userName,
            user_animationData: this.animation.export()
        })
        this.getFeeList()
        this.getImprestInfo()
    },
    setYear:function (e) {
        console.log("setYear-->",e)
       
        let yearIndex = e.target.dataset.index; //当前点击的索引
        let selectYear = e.target.dataset.year;
        this.toggleAnimation(0)
        this.setData({
            cityShow: '0',
            yearIndex,
            selectYear,
            year_animationData: this.animation.export()
        })
        this.getFeeList()
    },
    setMonth:function (e) {
        console.log("setMonth-->",e)
       
        let monthIndex = e.target.dataset.index; //当前点击的索引
        let selectMonth = e.target.dataset.month;
        this.toggleAnimation(0)
        this.setData({
            cityShow: '0',
            monthIndex,
            selectMonth,
            month_animationData: this.animation.export()
        })
        this.getFeeList()
    },
    cancelSelect: function(){
        this.pickUp()
        this.setData({
          cityShow: '0'
        })
    },
    pickUp: function(){
        if (this.data.cityShow == '4') {
            this.toggleAnimation(0)
            this.setData({
              user_animationData: this.animation.export()
            })
        } else if (this.data.cityShow == '5') {
          this.toggleAnimation(0)
          this.setData({
            year_animationData: this.animation.export()
          })
        } else if (this.data.cityShow == '6') {
          this.toggleAnimation(0)
          this.setData({
            month_animationData: this.animation.export()
          })
        }
    },
    getUserList() {
        let that = this
        lbbTaskService.getDeskUserList(null,function(res){
            if (res.data.length == 1) {
                that.setData({
                    userId: res.data[0].id,
                    userName: res.data[0].name
                })
                that.getFeeList()
            } else {
                wx.getUserInfo({
                    success(res) {
                        that.setData({
                            userName: res.userInfo.nickName,
                        })
                    }
                })
            }
            that.setData({
                userList:res.data
            })
        })
    },
    getFeeList() {
        let that = this
        console.log(that.data.selectYear)
        let params = {
            userId:that.data.userId,
            year:that.data.selectYear,
        }
        if (that.data.selectMonth!='全部月份') {
            params['month'] = that.data.selectMonth
        }
        wx.showLoading({
            title: '加载中',
          }) 
        lbbTaskService.getFeeStatistics(params,function(res){
            console.log(res.data)
            that.setData({
                feeList:res.data
            })
        })
    },
    nagativeToNext(e) {
        console.log(e)
        wx.navigateTo({
          url: '/pkgCost/pages/claim-expenses/index?userId='+this.data.userId+'&month='+e.currentTarget.dataset.month,
        })
    },
    //获取备用金金额
    getImprestInfo(){
        let that=this
        wx.showLoading({
            title: '加载中',
          }) 
        lbbTaskService.getImprestInfo({userId:that.data.userId},function (res) {
            that.setData({
                totalImpreset:res.data.totalImpreset,
                impresetBalance:res.data.impresetBalance
            })
        })
    },
    addFee(e) {
        // wx.showModal({
        //   title: '',
        //   content: '功能开发中。。。',
        //   complete: (res) => {
        //     if (res.cancel) {
              
        //     }
        
        //     if (res.confirm) {
              
        //     }
        //   }
        // })
        if(this.data.userId!=-1) {
            wx.navigateTo({
                url: '/pkgCost/pages/expenses-appeal/index?userId='+this.data.userId,
              })
        } else {
            wx.showToast({
              title: '请选择用户',
            })
        }
        
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getUserList()
        this.getFeeList()
        this.getImprestInfo()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
        let that = this
        if(!app.globalData.windowHeight){
            wx.getSystemInfo({
              complete: (res) => {
                app.getWindowHeight(res);
                that.setData({
                  scrollHeight:app.getScrollHeight(200)
                })
              },
            })
          }
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})